package com.xk857.forum.mapper;

import com.xk857.entities.Replay;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 星空小屋-回复论坛信息表 Mapper 接口
 *
 * @author CV大魔王
 * @since 2021-02-23
 */
public interface ReplayMapper extends BaseMapper<Replay> {


    /**
     * 通过话题id递归查询所有回答 这里先查询一级回答
     * @param forumId 话题id
     * @return Replay集合
     */
    @Select("SELECT id,parent_id,user_id,nick_name,user_image,forum_id,md_content,html_content,create_date FROM xk857_replay WHERE parent_id = -1 AND forum_id = #{forumId} ORDER BY create_date DESC")
    @Results({
            @Result(column = "id",property = "id",id = true),
            @Result(column = "parent_id",property = "parentId"),
            @Result(column = "id", property = "children", javaType = List.class, many = @Many(select = "com.xk857.forum.mapper.ReplayMapper.findReplayListByParentId"))
    })
    List<Replay> findByForumId(String forumId);


    /**
     * 根据父id查询子评论
     * @param parentId 父id
     * @return Replay集合
     */
    @Select("SELECT * FROM xk857_replay WHERE parent_id = #{parentId}")
    List<Replay> findReplayListByParentId(String parentId);

}
